<template>
  <div class="home-page">
    <!-- 顶部导航 -->
    <el-header class="app-header">
      <div class="header-content">
        <div class="logo">
          <h1>墨迹时光</h1>
          <span class="subtitle">传记写作助手</span>
        </div>
        
        <!-- 用户信息 -->
        <div class="user-section" v-if="authStore.isLoggedIn">
          <el-dropdown @command="handleUserCommand">
            <span class="el-dropdown-link">
              <el-avatar :src="authStore.user?.avatar" :size="32">
                {{ authStore.user?.name?.charAt(0) }}
              </el-avatar>
              <span class="username">{{ authStore.user?.name }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人资料</el-dropdown-item>
                <el-dropdown-item command="drafts">我的草稿</el-dropdown-item>
                <el-dropdown-item command="bookmarks">我的书签</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        
        <div class="login-section" v-else>
          <el-button type="primary" @click="showLoginDialog = true">登录</el-button>
        </div>
      </div>
    </el-header>

    <!-- 主要内容区域 -->
    <el-container class="main-container">
      <!-- 侧边栏 -->
      <el-aside width="250px" class="sidebar">
        <el-menu :default-active="activeMenu" @select="handleMenuSelect" class="sidebar-menu">
          <el-menu-item index="writing">
            <el-icon><Edit /></el-icon>
            <span>开始写作</span>
          </el-menu-item>
          <el-menu-item index="community">
            <el-icon><ChatDotRound /></el-icon>
            <span>社区分享</span>
          </el-menu-item>
          <el-menu-item index="drafts" v-if="authStore.isLoggedIn">
            <el-icon><Document /></el-icon>
            <span>我的草稿</span>
          </el-menu-item>
          <el-menu-item index="bookmarks" v-if="authStore.isLoggedIn">
            <el-icon><Star /></el-icon>
            <span>我的书签</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="content-area">
        <router-view />
      </el-main>
    </el-container>

    <!-- 登录对话框 -->
    <el-dialog v-model="showLoginDialog" title="用户登录" width="400px">
      <el-form :model="loginForm" ref="loginFormRef" :rules="loginRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" class="login-input" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" class="login-input" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showLoginDialog = false">取消</el-button>
          <el-button type="primary" @click="handleLogin">登录</el-button>
        </span>
      </template>
      <div class="login-tip">
        <el-text size="small" type="info">测试账号: user1, 密码: 123456</el-text>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const authStore = useAuthStore()

const activeMenu = ref('writing')
const showLoginDialog = ref(false)
const loginFormRef = ref()

const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleMenuSelect = (index) => {
  activeMenu.value = index
  router.push(`/${index}`)
}

const handleUserCommand = (command) => {
  switch (command) {
    case 'logout':
      authStore.logout()
      ElMessage.success('已退出登录')
      if (activeMenu.value === 'drafts' || activeMenu.value === 'bookmarks') {
        router.push('/writing')
        activeMenu.value = 'writing'
      }
      break
    case 'profile':
      ElMessage.info('个人资料功能开发中')
      break
    case 'drafts':
      router.push('/drafts')
      activeMenu.value = 'drafts'
      break
    case 'bookmarks':
      router.push('/bookmarks')
      activeMenu.value = 'bookmarks'
      break
  }
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate((valid) => {
    if (valid) {
      const success = authStore.login(loginForm.value.username, loginForm.value.password)
      if (success) {
        ElMessage.success('登录成功')
        showLoginDialog.value = false
        loginForm.value = { username: '', password: '' }
      } else {
        ElMessage.error('用户名或密码错误')
      }
    }
  })
}

onMounted(() => {
  authStore.initAuth()
  // 根据当前路由设置活动菜单
  const currentPath = router.currentRoute.value.path
  if (currentPath === '/community') {
    activeMenu.value = 'community'
  } else if (currentPath === '/drafts') {
    activeMenu.value = 'drafts'
  } else if (currentPath === '/bookmarks') {
    activeMenu.value = 'bookmarks'
  } else {
    activeMenu.value = 'writing'
  }
})
</script>

<style scoped>
.home-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.logo h1 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
}

.logo .subtitle {
  font-size: 12px;
  opacity: 0.8;
}

.user-section .el-dropdown-link {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.username {
  color: white;
}

.main-container {
  flex: 1;
  overflow: hidden;
}

.sidebar {
  background: #f5f5f5;
  border-right: 1px solid #e0e0e0;
}

.sidebar-menu {
  border-right: none;
}

.content-area {
  background: white;
  padding: 20px;
  overflow-y: auto;
}

.login-tip {
  text-align: center;
  margin-top: 10px;
}

.login-input {
  width: 100%;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
